<?php
class Controller_Demo extends  Controller_Template {
	public function __construct($req, $res) {
		$this->template = "smarty:effect/effects";
		parent::__construct($req, $res);
	}

    public function action_daily ($day, $type, $subtype)
    {
		$this->template->set_filename('smarty:effect/demo/daily');
        $this->template->set('dates', array($day));
        $this->template->set('channels', array('lenovo001'));
        if ($subtype == 'game')
            $this->template->set('pnames', array('怪物X联盟', '忘仙', '君王2'));
        else if ($subtype == 'ads')
            $this->template->set('pnames', array('QQ浏览器', '新浪微博'));
        $this->template->set('subtype', $subtype);
    }

    /**
     * 推广效果过滤选择器
     *
     */
    public function action_effect_filter ()
    {
        $query      = $this->request->query();
        $request    = Request::$initial->query();
        $manager    = Arr::get($request, 'manager_id');
        $activity   = Arr::get($request, 'activity_id');

        if (site::type() == 'manager')
        {
            $user_info = user::current_user()->get_user_info();
            if ($user_info['role_id'] == 'auditor')
                $manager = $user_info['uid'];
        }

        if (! isset($request['start_date']) && isset($query['start_date']))
            $request['start_date'] = $query['start_date'];

        if (! isset($request['end_date']) && isset($query['end_date']))
            $request['end_date'] = $query['end_date'];

        $activities = array();
        if ($manager > 0)
        {
            $result = Promotion_Activity::activity_by_manager($manager);
            foreach ($result as $act)
            {
                $activities[] = array(
                    'id' => Arr::get($act, 'id'),
                    'name' => Arr::get($act, 'name'),
                );
            }
        }
        else if (site::type() == 'merchant')
        {
            $activities = Promotion::activities_by_merchant(user::current_user_id());
        }

        $channels   = array();
        if ($activity > 0)
        {
            $result = Promotion::channels_by_activity ($activity);
            foreach ($result as $chn)
            {
                $channels[] = array(
                'unique_key'   => Arr::get($chn, 'unique_key'),
                'name'         => Arr::get($chn, 'name'),
                );
            }
        }

        array_unshift($activities, array(
                'id'   => 0,
                'name' => '全部活动',
            ));

        $channels = array(
            array(
                'unique_key'   => 'ac.union.lenovo001',
                'name'         => 'lenovo001',
            ),
        );

        array_unshift($channels, array(
                'id'         => 0,
                'unique_key' => '',
                'name'       => '全部渠道',
            ));

        $this->template->set_filename('smarty:effect/demo/widget_filter');
		$this->template->set('activities', $activities);
		$this->template->set('channels',   $channels);
        $this->template->set('managers',   Promotion_Activity::managers());
        $this->template->set('request',    $request);
    }



	/*
	 * 获得所有相关的推广活动效果
	 *
	 *
	 */	
	public function action_all()
	{
		$mer_id = User::current_user_id();
		if (site::type() == 'manager')
		{
			$mer_id = NULL;
		}

		$start_date = $this->request->query('start_date');
		$end_date = $this->request->query('end_date');
		if($start_date == NULL){
			$start_date = date('Y-m-d', strtotime('-7 days')); 
		}
		if($end_date == NULL){
			$end_date = date('Y-m-d',strtotime('-1 days'));
		}

        $activity_id  = $this->get_activity_id ();
		$channel_id   = $this->request->query('channel_id') ;	
		$effect       = new Promotion_Effect;
		$summary_data = $effect->get_summary_data($start_date,$end_date,$activity_id,$channel_id,$mer_id);
		$rs = array();
		foreach($summary_data as $key => $val)
		{
			$rs[$key]['activity_id'] = $val['activity_id'];  
			$rs[$key]['channel_id'] = $val['channel_id'];  
			$rs[$key]['count'] = $val['count'];  
			$rs[$key]['real_counts'] = $val['real_counts'];  
			$rs[$key]['merchant_id'] = $val['merchant_id'];  
			$channel = Promotion_channel::info($val['channel_id']);
			$activity = Promotion_Activity::get_activity_by_id($val['activity_id']);
			$rs[$key]['channel_name'] = $channel['name'];
			$rs[$key]['activity_name'] = $activity['name']; 
			$rs[$key]['date'] = date('Y-m-d',strtotime('-1 days'));
			$rs[$key]['price'] = $activity['price'];
			$rs[$key]['pay_param'] = $activity['pay_param'];
			$rs[$key]['manager_name'] = User::name($activity['creater'],'realname');
		}

	    $this->template->set('start_date',str_replace('-','/',$start_date));
		$this->template->set('end_date',str_replace('-','/',$end_date));

        $dates = array();
        for ($d = strtotime($start_date); $d <= strtotime($end_date); $d += 86400 )
        {
            $dates[] = date("Y-m-d", $d);
        }

        $this->template->set('dates', $dates);

		$this->template->set_filename('smarty:effect/demo/effects');
		$out_date = substr($start_date,5) . '...' . substr($end_date,5);
		$this->template->set('out_date',$out_date);
		$this->template->set('effects',$rs);
	}

	/**
	 *
	 * 不同维度获得效果
	 *
	 */
	public function action_data()
	{
		$get         = $this->request->query();
		$channel_id  = Arr::get($get, 'channel_id',  0);
		$activity_id = Arr::get($get, 'activity_id', 0);
		$channel_key = Arr::get($get, 'channel_key', NULL);
		$merchant_id = Arr::get($get, 'merchant_id', 0);
        $manager_id  = Arr::get($get, 'manager_id',  0);
		$manager_real_name = User::name($manager_id,'realname');
		$start_date  = str_replace('/','-',Arr::get($get,'start_date',NULL));
		$end_date    = str_replace('/','-',Arr::get($get,'end_date',NULL));
        
		if ($activity_id == 0 && $channel_key == NULL && $manager_id == 0)
		{
			$this->request->redirect('/effect/all/');
		}
        

        if($start_date == NULL && $end_date==NULL)
        {
			$start_date = date('Y-m-d', strtotime("-7 days"));	
			$end_date   = date('Y-m-d', strtotime("-1 days"));	
		}

		if($channel_key != NULL)
		{
			$channel_info = Promotion_Channel::info_key($channel_key);
			$channel_id   = Arr::get($channel_info, 'id', '0');
		}

		$query = array(
			'include_channel_info'  => TRUE,
			'include_activity_info' => TRUE,
			'include_merchant_info' => TRUE,
			'day_greater'           => $start_date,
			'day_less'              => $end_date,
			'order_by'              => array('day' => 'DESC',),
		);

		if($merchant_id > 0 )
			$query['merchant'] = $merchant_id;

		if($channel_id > 0)
        {
			$this->template->set('isChannel',TRUE);
			$query['channel'] = $channel_id;
        }

		if($activity_id > 0)
		{
			$this->template->set('isActivity',TRUE);
			$query['activity'] = $activity_id;
		}
        else if ($manager_id > 0)
        {
			$query['activity'] = $this->activity_ids_by_manager ($manager_id);
        }

		$rs = Promotion_Effect::search($query, NULL, NULL);
		$list = array();
		$val = $rs['list'];
		if($channel_id == 0)
		{
			//如果不传channl_id,展示活动汇总的信息
			foreach($val as $list_val)
			{
				$day = $list_val['day'];
				$activity_id = $list_val['activity_id'];	
				$list[$day]['counts'] += $list_val['counts'];
				$list[$day]['real_counts']  += $list_val['real_counts'];
				$list[$day]['day'] = $list_val['day'];
				$list[$day]['activity_info'] = $list_val['activity_info'];
				$list[$day]['merchant_info'] = $list_val['merchant_info'];
			}
		}
		if($activity_id == 0){
			//如果不传activity_id,展示活动汇总的信息
			foreach($val as $list_val)
			{
				$day = $list_val['day'];
				$list[$day]['counts']       += $list_val['counts'];
				$list[$day]['real_counts']  += $list_val['real_counts'];
				$list[$day]['day']           = $list_val['day'];
				$list[$day]['channel_info']  = $list_val['channel_info'];
				$list[$day]['merchant_info'] = $list_val['merchant_info'];
			}
		}
		if(empty($list)){
			$list = $rs['list'];
		}

        $summary = $this->build_summary_counts ($list);
		$this->template->set_filename('smarty:effect/effects_data');
		$this->template->set('start_date',str_replace('-','/',$start_date));
		$this->template->set('end_date',str_replace('-','/',$end_date));
		$this->template->set('list',$list);
        $this->template->set('summary', $summary);
		$this->template->set('channel_id',$channel_id);
		$this->template->set('activity_id',$activity_id);
		$this->template->set('manager_name',$manager_real_name);

	}

    private function get_activity_id ()
    {
		$activity_id  = $this->request->query('ac_id') ;	
        if ($activity_id > 0)
            return $activity_id;

        if (site::type() == 'manager')
        {
            $user_info = user::current_user()->get_user_info();
            if ($user_info['role_id'] == 'auditor')
            {
                $result = Promotion_Activity::activity_by_manager($user_info['uid']);
                if (empty($result))
                    return -1;
                else
                    return $result[0]['id'];
            }
        }
        else
        {
            return $activity_id;
        }
    }

    private function activity_ids_by_manager ($manager_id)
    {
        $acts = Promotion_Activity::activity_by_manager($manager_id);
        $ids  = array(-1,);
        foreach ($acts as $act)
            $ids[] = $act['id'];

        return $ids;
    }

    private function build_summary_counts ($list)
    {
        if (empty($list) || ! is_array($list))
            return array('counts' => 0, 'real_counts' => 0);

        $counts = $real_counts = 0;
        foreach ($list as $day => & $rs)
        {
            $counts      += $rs['counts'];
            $real_counts += $rs['real_counts'];
        }

        return array(
            'counts'      => $counts,
            'real_counts' => $real_counts,
        );
    }

	public function action_all_eff()
	{

		$day = date('Y-m-d', strtotime("-1 days"));
		$query = array(
			'merchant' => User::current_user_id(),
			'day_less' => $day,
			'day_greater' => $day,
			'include_channel_info'  => TRUE,
			'include_activity_info' => TRUE,
		);
		$rs = Promotion_Effect::search($query, NULL, NULL);
	}
}
